package com.firstresponder.activity;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class DirectionPathOverlay extends ItemizedOverlay<OverlayItem> {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context context; 
    private GeoPoint gp1;
    private GeoPoint gp2;
    private static FirstResponder MAIN_ACTIVITY = null;

    public DirectionPathOverlay(Drawable defaultMarker,Context context,GeoPoint gp1, GeoPoint gp2) {
    	this(defaultMarker);
    	this.context=context;
        this.gp1 = gp1;
        this.gp2 = gp2;
    }
    

    public DirectionPathOverlay(Drawable defaultMarker) {
    	super(boundCenterBottom(defaultMarker));
		// TODO Auto-generated constructor stub
    	
	}
  
    @Override
	protected boolean onTap(int index) {
		OverlayItem item = mOverlays.get(index);
//		AlertDialog.Builder dialog = new AlertDialog.Builder(context);
//		dialog.setTitle(item.getTitle());
//	    dialog.setMessage(item.getSnippet());
//		dialog.show();
		Toast toast=new Toast(MAIN_ACTIVITY);
		toast=Toast.makeText(MAIN_ACTIVITY, item.getSnippet(),Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toast.show();
		return true;

		
	}
	@Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        // TODO Auto-generated method stub
        Projection projection = mapView.getProjection();
        if (shadow == false) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            Point point = new Point();
            projection.toPixels(gp1, point);
            paint.setColor(0xFF46C4D7);
            paint.setAlpha(150);
            Point point2 = new Point();
            projection.toPixels(gp2, point2);
            paint.setStrokeWidth(5);
            canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
                    (float) point2.y, paint);
        }
        return super.draw(canvas, mapView, shadow, when);
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        // TODO Auto-generated method stub

        super.draw(canvas, mapView, shadow);
    }

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mOverlays.size();
	}
	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	    
	}
	 public void removeOverlay(OverlayItem overlay) {
	        mOverlays.remove(overlay);
	        populate();
	    }


	    public void clear() {
	        mOverlays.clear();
	        populate();
	    }
	public static void setMainActivity(FirstResponder activity) {
		MAIN_ACTIVITY = activity;
	}

}